<?php

define("LIBERATIONSWEB", 20091223);

session_start();

//Librairie de fonctions
require_once(__DIR__ . '/vendor/autoload.php');
require_once("lib/libconfig.inc.php");
require_once("lib/libsession.inc.php");
require_once("lib/libfonctions.inc.php");
require_once("lib/libmysql.inc.php");
require_once("lib/libencode.inc.php");
require_once("lib/libcourriel.inc.php");
require_once("lib/libliberations.inc.php");
require_once("lib/libcentrale.inc.php");
require_once("fpdf/fpdf.php");

class PDF extends FPDF {

    var $NomEmploye;
    var $NomEmployeur;
    var $DateLiberation;
    var $Commentaire;
    var $CalculDuree;
    var $Nombre;
    var $TOperation;
    var $NoArticle;
    var $Texte24;
    var $Texte27;
    var $Texte19;
    var $Texte20;
    var $syndicatnom;
    var $configCentrale;

    function WordWrap(&$text, $maxwidth) {
        $text = trim($text);
        if ($text === '')
            return 0;
        $space = $this->GetStringWidth(' ');
        $lines = explode("\n", $text);
        $text = '';
        $count = 0;

        foreach ($lines as $line) {
            $words = preg_split('/ +/', $line);
            $width = 0;

            foreach ($words as $word) {
                $wordwidth = $this->GetStringWidth($word);
                if ($wordwidth > $maxwidth) {
                    // Word is too long, we cut it
                    for ($i = 0; $i < strlen($word); $i++) {
                        $wordwidth = $this->GetStringWidth(substr($word, $i, 1));
                        if ($width + $wordwidth <= $maxwidth) {
                            $width += $wordwidth;
                            $text .= substr($word, $i, 1);
                        } else {
                            $width = $wordwidth;
                            $text = rtrim($text) . "\n" . substr($word, $i, 1);
                            $count++;
                        }
                    }
                } elseif ($width + $wordwidth <= $maxwidth) {
                    $width += $wordwidth + $space;
                    $text .= $word . ' ';
                } else {
                    $width = $wordwidth + $space;
                    $text = rtrim($text) . "\n" . $word . ' ';
                    $count++;
                }
            }
            $text = rtrim($text) . "\n";
            $count++;
        }
        $text = rtrim($text);
        return $count;
    }

    // ****************************************************
    // Entête de page
    // ****************************************************
    function Header() {
        $Image41 = 'logo/' . $_SESSION["syndicat"] . "." . $_SESSION['logo'];
        $this->Image($Image41, 139, 25, 47, 22);

        $etiquette18 = "Compilation des soldes";
        $this->SetXY(26, 26);
        $this->SetFont('Times', 'BI', 20);
        $this->Cell(114, 8, $etiquette18, 0, 0, 'L');

        $this->SetLineWidth(1);
        $this->Line(25, 53, 189, 53);

        $this->SetXY(26, 47);
        $this->SetFont('Times', 'BI', 11);
        $NoArticle_etiquette = "Article";
        $this->Cell(14, 5, $NoArticle_etiquette, 0, 0, 'L');

        $this->SetXY(42, 47);
        $this->SetFont('Times', 'BI', 11);
        $NomEmployeur_etiquette = "Employeur";
        $this->Cell(40, 5, $NomEmployeur_etiquette, 0, 0, 'L');

        $this->SetXY(116, 45);
        $this->SetFont('Times', 'BI', 11);
        $Nombre_etiquette = "Selon la convention";
        $this->WordWrap($Nombre_etiquette, 22);
        $this->MultiCell(22, 4, $Nombre_etiquette);

        $this->SetXY(139, 47);
        $this->SetFont('Times', 'BI', 11);
        $calcul_etiquette = "Utilisation";
        $this->Cell(18, 5, $calcul_etiquette, 0, 0, 'C');

        $this->SetXY(160, 47);
        $this->SetFont('Times', 'BI', 11);
        $expression_etiquette = "Solde";
        $this->Cell(14, 5, $expression_etiquette, 0, 0, 'C');

        $this->SetY(55);
    }

    // ****************************************************
    // Pied de page
    // ****************************************************
    function Footer() {
        //Numéro de page
        //Positionnement à 1,5 cm du bas
        //Police Arial italique 8
        $this->SetY(-12);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');

        $etiquette43 = $this->configCentrale["syndicat_nom"];
        $etiquette45 = $this->configCentrale["adresse"] . ", " . $this->configCentrale["ville"] .
                " (" . $this->configCentrale["province"] . ") " . $this->configCentrale["codepostal"] .
                " Téléphone : " . str_replace(array("(", ")"), "", $this->configCentrale["telephone"]) .
                " Télécopieur : " . str_replace(array("(", ")"), "", $this->configCentrale["telecopieur"]);

        $this->SetLineWidth(0);
        $this->Line(25, 264, 184, 264);

        $this->SetXY(25, 259);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(160, 4, mb_convert_encoding($etiquette43, 'ISO-8859-1', 'UTF-8'), 0, 0, 'C');

        $this->SetXY(25, 264);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(160, 4, mb_convert_encoding($etiquette45, 'ISO-8859-1', 'UTF-8'), 0, 0, 'C');
    }
}

//Exécution de la requête SQL provenant de la compilation du solde.
header('Content-Type: text/html; charset=$ENCODAGE');

$connection = setConnectionSql();

$sqlprinc = $_SESSION['requêteSQL'];
mysqli_query($connection, "SET SQL_BIG_SELECTS=1");
$result = mysqli_query($connection, "$sqlprinc") or die("La requête (3) a échouée.");

//Instanciation de la classe dérivée
$pdf = new PDF('P', 'mm', 'Letter');
$pdf->SetMargins(25, 25);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->syndicatnom = $_SESSION["syndicatnom"];
$pdf->configCentrale = buildConfigCentrale();

$nomemploye = "";
$nomemployeur = "";
$calculduree = 0;
$nombredeligne = 0;
$calculduree2 = 0;
$nombredeligne2 = 0;

for ($i = 1; $i <= mysqli_num_rows($result); $i++) {
    $champs = mysqli_fetch_row($result);

    $valeur1 = $champs[1];
    $valeur2 = mb_convert_encoding($champs[2], 'ISO-8859-1', 'UTF-8');

    $valeur3 = sprintf("%1.1f", $champs[3]);
    $valeur4 = sprintf("%1.1f", $champs[4]);
    $valeur5 = sprintf("%1.1f", $champs[5]);

    $pdf->SetX(26);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(14, 4, $valeur1, 0, 0, 'L');

    $pdf->SetX(42);
    $pdf->Cell(70, 4, $valeur2, 0, 0, 'L');

    $pdf->SetX(139);
    $pdf->Cell(14, 5, $valeur3, 0, 0, 'C');

    $pdf->SetX(116);
    $pdf->Cell(14, 5, $valeur4, 0, 0, 'C');

    $pdf->SetX(160);
    if ($champs[5] < 0) {
        $pdf->SetTextColor(255, 0, 0);
    }
    $pdf->Cell(14, 5, $valeur5, 0, 0, 'C');
    if ($champs[5] < 0) {
        $pdf->SetTextColor(0, 0, 0);
    }

    $pdf->ln();
}

$pdf->Output();
